package cn.t.repository.impl.user;

import cn.t.converter.user.UserPassportConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.UserPassportDO;
import cn.t.persistence.entity.user.UserPassportPO;
import cn.t.persistence.mapper.user.UserPassportMapper;
import cn.t.repository.user.UserPassportRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通行证表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class UserPassportRepositoryImpl extends RepositoryImpl<UserPassportMapper, UserPassportPO, UserPassportConverter, UserPassportDO> implements UserPassportRepository {


    @Override
    public List<UserPassportDO> findByModel(UserPassportDO userPassportDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<UserPassportPO> list = getWrapper(userPassportDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<UserPassportPO> getWrapper(UserPassportDO userPassportDO) {
        LambdaQueryChainWrapper<UserPassportPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(userPassportDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(userPassportDO.getGmtCreatedEnd())) {
            wrapper.between(UserPassportPO::getGmtCreated, userPassportDO.getGmtCreatedStart(), userPassportDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(userPassportDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(userPassportDO.getGmtModifiedEnd())) {
            wrapper.between(UserPassportPO::getGmtModified, userPassportDO.getGmtModifiedStart(), userPassportDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(userPassportDO.getUserId())) {
            wrapper.eq(UserPassportPO::getUserId, userPassportDO.getUserId());
        }
        if (!ObjectUtils.isEmpty(userPassportDO.getPassportType())) {
            wrapper.eq(UserPassportPO::getPassportType, userPassportDO.getPassportType());
        }
        if (!ObjectUtils.isEmpty(userPassportDO.getPassportValue())) {
            wrapper.eq(UserPassportPO::getPassportValue, userPassportDO.getPassportValue());
        }
        if (!ObjectUtils.isEmpty(userPassportDO.getFirmId())) {
            wrapper.eq(UserPassportPO::getFirmId, userPassportDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(userPassportDO.getPassportStatus())) {
            wrapper.eq(UserPassportPO::getPassportStatus, userPassportDO.getPassportStatus());
        }
        return wrapper;
    }

    @Override
    public long count(UserPassportDO userPassportDO) {
        return getWrapper(userPassportDO).count();
    }

    @Override
    public boolean deletePassportById(Long passportId) {
        return lambdaUpdate().set(UserPassportPO::getPassportStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(UserPassportPO::getIsDeleted, String.valueOf(YesNoEnum.TRUE.getCode()))
                .set(UserPassportPO::getGmtModified, LocalDateTime.now()).eq(UserPassportPO::getId, passportId).update();
    }

    @Override
    public void disablePassportByUserId(Long userId) {
        lambdaUpdate().set(UserPassportPO::getPassportStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(UserPassportPO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()))
                .set(UserPassportPO::getGmtModified, LocalDateTime.now()).eq(UserPassportPO::getUserId, userId).update();
    }

    @Override
    public void enablePassportByUserId(Long userId) {
        lambdaUpdate().set(UserPassportPO::getPassportStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()))
                .set(UserPassportPO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()))
                .set(UserPassportPO::getGmtModified, LocalDateTime.now()).eq(UserPassportPO::getUserId, userId).update();
    }
}
